package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.entity.SalaryBaseItemize;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.service.ISalaryBaseItemizeService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/salaryBaseItemizeController", produces = MediaType.APPLICATION_JSON_VALUE)
public class SalaryBaseItemizeController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryBaseItemizeController.class);

    @Autowired
    private ISalaryBaseItemizeService iSalSalaryBaseItemizeService;

    @Autowired
    private ICommonService iCommonService;

    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    /**
     * 查询分页数据
     *
     * @return
     */
    @RequestMapping("/querySalaryBaseItemizePage")
    public ApiResultDTO querySalaryBaseItemizePage(@RequestHeader("X-Token") String tokenid,
                                                   @RequestParam(value = "page") String page,
                                                   @RequestParam(value = "limit") String limit,
                                                   @RequestParam(value = "agencyId", required = false) String agencyId,
                                                   @RequestParam(value = "salaryBaseId", required = false) String salaryBaseId,
                                                   @RequestParam(value = "personState", required = false) String personState,
                                                   @RequestParam(value = "payeeAcctId", required = false) String payeeAcctId,
                                                   @RequestParam(value = "payeeAcctBankName", required = false) String payeeAcctBankName) {
        try {
            UserDTO userDto = getUser(tokenid);
            List<String> childrenId = iCommonService.getAgencyChildrenId(userDto, agencyId,tokenid);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            Map<String, Object> queryParam = new HashMap<>();
            queryParam.put("childrenId", childrenId);
            queryParam.put("salaryBaseId", salaryBaseId);
            queryParam.put("personState", personState);
            queryParam.put("payeeAcctId", payeeAcctId);
            queryParam.put("payeeAcctBankName", payeeAcctBankName);
            queryParam.put("mof_div_code", userDto.getProvince());
            PageDTO<SalaryBaseItemize> pageDTO = iSalSalaryBaseItemizeService.querySalaryBaseItemizePage(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 查询单机构下数据
     *
     * @return
     */
    @RequestMapping("/querySalaryBaseItemize")
    public ApiResultDTO querySalaryBaseItemize(@RequestHeader("X-Token") String tokenid,
                                               @RequestParam(value = "agencyId", required = false) String agencyId) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("mof_div_code", userDTO.getProvince());
            queryParam.put("agencyId", agencyId);
            List<SalaryBaseItemize> pageDTO = iSalSalaryBaseItemizeService.querySalaryBaseItemize(queryParam);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 人员类型
     *
     * @return
     */
    @RequestMapping("/queryBaseDataList")
    public ApiResultDTO queryBaseDataList(@RequestHeader("X-Token") String tokenid) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("mof_div_code", userDTO.getProvince());
            List<Map<String, Object>> list = iSalSalaryBaseItemizeService.queryBaseDataList(queryParam);
            return ApiResultDTO.success(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 收款帐号
     *
     * @return
     */
    @RequestMapping("/queryPayeeAcctList")
    public ApiResultDTO queryPayeeAcctList(@RequestHeader("X-Token") String tokenid) {
        try {
            UserDTO userDTO = getUser(tokenid);
            List<Map<String, Object>> list = iSalSalaryBaseItemizeService.queryPayeeAcctList(userDTO);
            return ApiResultDTO.success(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 收款项
     *
     * @param agencyId
     * @return
     */
    @RequestMapping("/querySalaryBasedataList")
    public ApiResultDTO querySalaryBasedataList(@RequestHeader("X-Token") String tokenid, @RequestParam(value = "agencyId", required = false) String agencyId) {
        try {
            UserDTO userDTO = getUser(tokenid);
            List<Map<String, Object>> list = iSalSalaryBaseItemizeService.querySalaryBasedataList(agencyId, userDTO);
            return ApiResultDTO.success(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 新增
     *
     * @param salaryBaseItemizes
     * @return
     */
    @PostMapping("/saveSalaryBaseItemize")
    public ApiResultDTO saveSalaryBaseItemize(@RequestHeader("X-Token") String tokenid,
                                              @RequestBody List<SalaryBaseItemize> salaryBaseItemizes) {
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs = new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("财务审核--指标工资项配置--新增");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);
            iSalSalaryBaseItemizeService.insertSalaryBaseItemize(salaryBaseItemizes, userDTO);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 工资项配置发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 工资项配置发生异常");
        }
    }

    /**
     * 批量新增
     *
     * @return
     */
    @PostMapping("/saveSalaryBaseItemizeBatch")
    public ApiResultDTO saveSalaryBaseItemizeBatch(@RequestHeader("X-Token") String tokenid,
                                                   @RequestBody SalaryBaseItemize salaryBaseItemize) {
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs = new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("财务审核--指标工资项配置--批量新增");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);
            //添加区划
            salaryBaseItemize.setMofDivCode(userDTO.getProvince());
            iSalSalaryBaseItemizeService.saveSalaryBaseItemizeBatch(salaryBaseItemize, userDTO);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 工资项配置发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 工资项配置发生异常");
        }
    }
    /**
     * 复制指标工资项
     *
     * @return
     */
    @PostMapping("/copySalaryBaseItemizeBatch")
    public ApiResultDTO copySalaryBaseItemizeBatch(@RequestHeader("X-Token") String tokenid,
                                                   @RequestBody SalaryBaseItemize salaryBaseItemize) {
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs = new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("财务审核--指标工资项配置--复制");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);
            //添加区划
            salaryBaseItemize.setMofDivCode(userDTO.getProvince());
            iSalSalaryBaseItemizeService.copySalaryBaseItemizeBatch(salaryBaseItemize, userDTO);
            return ApiResultDTO.success(null, "复制成功");
        } catch (Exception e) {
            logger.error("复制 工资项配置发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "复制 工资项配置发生异常");
        }
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSalaryBaseItemize/{id}")
    public ApiResultDTO deleteSalaryBaseItemize(@PathVariable("id") String id) {
        try {
            iSalSalaryBaseItemizeService.deleteSalaryBaseItemize(id);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 工资项配置发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 工资项配置发生异常");
        }
    }

    /**
     * 修改
     *
     * @param salaryBaseItemize
     * @return
     */
    @PostMapping("/updateSalaryBaseItemize")
    public ApiResultDTO updateSalaryBaseItemize(@RequestBody SalaryBaseItemize salaryBaseItemize) {
        try {
            iSalSalaryBaseItemizeService.updateSalaryBaseItemize(salaryBaseItemize);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改 工资项配置发生异常");
        }
    }


    /**
     * 根据条件获取单位信息
     *根据 salaCardBank 和 personState 过滤
     * @param id
     * @return
     */
    @GetMapping("/queryAgencyInfo")
    public ApiResultDTO queryAgencyInfo(@RequestHeader("X-Token") String tokenid,
                                        @RequestParam(value = "agencyId", required = false) String id,
                                        @RequestParam(value = "salaCardBank", required = false) String salaCardBank,
                                        @RequestParam(value = "personState", required = false) String personState) {
        try {
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenId = iCommonService.getAgencyChildrenId(userDTO, id,tokenid);
            List<AgencyInfo>  queryAgencyInfo=iSalSalaryBaseItemizeService.queryAgencyInfo(childrenId,salaCardBank,personState);
            return ApiResultDTO.success(queryAgencyInfo, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");

        }
    }
    @GetMapping("/deleteSalaryBaseItemizeAll")
    public ApiResultDTO deleteSalaryBaseItemizeAll(@RequestHeader("X-Token") String tokenid ) {
        try {
            UserDTO userDTO = getUser(tokenid);
           iSalSalaryBaseItemizeService.deleteSalaryBaseItemizeAll(userDTO);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "操作失败");

        }
    }
}